package thread;

//  synchronized : 直接修饰一个实例化方法
class Count {
    public int count = 0;

    synchronized public void add() {
            count++;
    }

    public void add2() {
        synchronized (this) {
            count++;
        }
    }
}

public class demo15_synchronized {
    public static void main(String[] args) throws InterruptedException {
        Count c = new Count();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                c.add();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                c.add();
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(c.count);
    }
}



